var methodValue = new Array("pad","repeat","reflect");

function linear_init(evt) 
{
    	gradient = svgdoc.getElementById("MyGradient")
	vector = svgdoc.getElementById("vector")
	x1 = 30
	y1 = 30
	x2 = 60
	y2 = 70 
	stop = new Array(0,0.2,0.5,0.8)
	nb_stops = 3
	color = new Array("white","red","yellow","blue")
	method = "pad"
	hue=0
	saturate=50
	luminosity=50
	clicked=false
	clicked2 = false
	clicked3 = false
	rect_color ="#BF4040"
	svgdoc.getElementById("gradient_header").firstChild.data = popup_message[14]
	svgdoc.getElementById("gradient_ok").firstChild.data = popup_message[4]
	svgdoc.getElementById("gradient_renonce").firstChild.data = popup_message[5]
	svgdoc.getElementById("popup15").firstChild.data = popup_message[15]
	svgdoc.getElementById("popup16").firstChild.data = popup_message[16]
	for (var i = 18 ; i <= 22 ; i ++)
		svgdoc.getElementById("popup" + i.toString()).firstChild.data = popup_message[i]
}

function radial_init(evt) 
{
	x2=100
	y2=50
    	gradient = svgdoc.getElementById("MyGradient");
	vector = svgdoc.getElementById("vector");
	cx = 50 
	cy = 50
	r = 50
	fx = 30 
 	fy = 30
 	stop = new Array(0,0.2,0.5,0.8)
	nb_stops = 3
	color = new Array("white","red","yellow","blue")
	method = "pad"
	hue=0
	saturate=50
	luminosity=50
	clicked=false
	clicked2 = false
	clicked3 = false
	rect_color ="#BF4040"
	svgdoc.getElementById("gradient_header").firstChild.data = popup_message[14]
	svgdoc.getElementById("gradient_ok").firstChild.data = popup_message[4]
	svgdoc.getElementById("gradient_renonce").firstChild.data = popup_message[5]
	svgdoc.getElementById("popup15").firstChild.data = popup_message[15]
	svgdoc.getElementById("popup17").firstChild.data = popup_message[17]
	for (var i = 18 ; i <= 22 ; i ++)
		svgdoc.getElementById("popup" + i.toString()).firstChild.data = popup_message[i]
}

function linear_vectorGradient(evt) 
{
	if (!clicked3)
		return
	if (ASV == "6")
	{
		var matrix = svgdoc.getElementById("test").getScreenCTM().inverse()
		xm = parseInt(evt.clientX * matrix.a + evt.clientY * matrix.c + matrix.e)
		ym = parseInt(evt.clientX * matrix.b + evt.clientY * matrix.d + matrix.f)
	}
	else
	{
		var matrix = svgdoc.getElementById(prefixe_tool).getCTM().inverse()
		var xm1 = parseInt(pan_x * (1 - 1 / zoom) + coordo_x(evt.clientX) / zoom)
		var ym1 = parseInt(pan_y * (1 - 1 / zoom) + coordo_y(evt.clientY) / zoom)
		xm = xm1 * matrix.a + ym1 * matrix.c + matrix.e
		ym = xm1 * matrix.b + ym1 * matrix.d + matrix.f - 15
	}
	var x = xm + 80;
	var y = ym 
        var target  = evt.target;
	target.setAttributeNS(null,"cx",x);
	target.setAttributeNS(null,"cy",y);
	var targetId = target.getAttributeNS(null,"id");
	if (targetId == "p1")
	{
		x1 = ( x - 100 ) / 3;
		y1 = y - 350;
		gradient.setAttributeNS(null,"x1",x1.toString()+"%");
		gradient.setAttributeNS(null,"y1",y1.toString()+"%");
		vector.setAttributeNS(null,"x1",x);
		vector.setAttributeNS(null,"y1",y);
	}
	if (targetId == "p2")
	{
		x2 = ( x - 100 ) / 3;
		y2 = y - 350;
		gradient.setAttributeNS(null,"x2",x2.toString()+"%");
		gradient.setAttributeNS(null,"y2",y2.toString()+"%");
		vector.setAttributeNS(null,"x2",x);
		vector.setAttributeNS(null,"y2",y);
	}
}

function radial_vectorGradient(evt) 
{
	if (!clicked3)
		return
	if (ASV == "6")
	{
		var matrix = svgdoc.getElementById("test").getScreenCTM().inverse()
		xm = parseInt(evt.clientX * matrix.a + evt.clientY * matrix.c + matrix.e)
		ym = parseInt(evt.clientX * matrix.b + evt.clientY * matrix.d + matrix.f)
	}
	else
	{
		var matrix = svgdoc.getElementById(prefixe_tool).getCTM().inverse()
		var xm1 = parseInt(pan_x * (1 - 1 / zoom) + coordo_x(evt.clientX) / zoom)
		var ym1 = parseInt(pan_y * (1 - 1 / zoom) + coordo_y(evt.clientY) / zoom)
		xm = xm1 * matrix.a + ym1 * matrix.c + matrix.e
		ym = xm1 * matrix.b + ym1 * matrix.d + matrix.f - 15
	}
	var x = xm
	var y = ym 
        var target  = evt.target;
	target.setAttributeNS(null,"cx",x);
	target.setAttributeNS(null,"cy",y);
	var targetId = target.getAttributeNS(null,"id");
		if (targetId == "p1")
			{
			cx = ( x - 50 ) / 2;
			cy = ( y - 350 ) / 2;
			gradient.setAttributeNS(null,"cx",cx.toString()+"%");
			gradient.setAttributeNS(null,"cy",cy.toString()+"%");
			vector.setAttributeNS(null,"cx",x);
			vector.setAttributeNS(null,"cy",y);
			r = Math.sqrt((x2-cx)*(x2-cx)+(y2-cy)*(y2-cy));
			gradient.setAttributeNS(null,"r",r.toString()+"%");
			vector.setAttributeNS(null,"r",r*2);
			}
		if (targetId == "p2")
			{
			x2 = ( x - 50 ) / 2;
			y2 = ( y - 350 ) / 2;
			r = Math.sqrt((x2-cx)*(x2-cx)+(y2-cy)*(y2-cy));
			gradient.setAttributeNS(null,"r",r.toString()+"%");
			vector.setAttributeNS(null,"r",r*2);
			}
		if (targetId == "p3")
			{
			fx = ( x - 50 ) / 2;
			fy = ( y - 350 ) / 2;
			gradient.setAttributeNS(null,"fx",fx.toString()+"%");
			gradient.setAttributeNS(null,"fy",fy.toString()+"%");
			}
}

function methodSpread(param) 
{
        methodChoosed = svgdoc.getElementById("method");
	methodChoosed.setAttributeNS(null,"cx",50 + param * 70);
	method = methodValue[param];
	gradient.setAttributeNS(null,"spreadMethod",method);
}

function offsetStop(evt) 
{
	if (!clicked2)
		return
	if (ASV == "6")
	{
		var matrix = svgdoc.getElementById("test").getScreenCTM().inverse()
		xm = parseInt(evt.clientX * matrix.a + evt.clientY * matrix.c + matrix.e)
		ym = parseInt(evt.clientX * matrix.b + evt.clientY * matrix.d + matrix.f)
	}
	else
	{
		var matrix = svgdoc.getElementById(prefixe_tool).getCTM().inverse()
		var xm1 = parseInt(pan_x * (1 - 1 / zoom) + coordo_x(evt.clientX) / zoom)
		var ym1 = parseInt(pan_y * (1 - 1 / zoom) + coordo_y(evt.clientY) / zoom)
		xm = xm1 * matrix.a + ym1 * matrix.c + matrix.e
		ym = xm1 * matrix.b + ym1 * matrix.d + matrix.f - 15
	}
	var x = xm + 390;
	if (x < 460 )
		x = 460
	if (x > 660 )
		x = 660
	var target  = evt.target;
	target.setAttributeNS(null,"cx",x);
	var offset = ( x - 460 ) / 2;
	var targetId = target.getAttributeNS(null,"id");
	var num_stop = parseInt(targetId.substring(1,2));
	var stopTarget = svgdoc.getElementById("st"+num_stop.toString());
	stopTarget.setAttributeNS(null,"offset",offset.toString()+"%");
	stop[num_stop] = offset/100;
	for (i=2;i<=nb_stops;i++)
	{
		if (stop[i] < stop[i-1])
			stop[i] = stop[i-1]
	}
}

function gradient_move_colorHSL(evt,num)
{if (clicked)
	{
		if (ASV == "6")
		{
			var matrix = svgdoc.getElementById("test").getScreenCTM().inverse()
			xm = parseInt(evt.clientX * matrix.a + evt.clientY * matrix.c + matrix.e)
			ym = parseInt(evt.clientX * matrix.b + evt.clientY * matrix.d + matrix.f)
		}
		else
		{
			var matrix = svgdoc.getElementById(prefixe_tool).getCTM().inverse()
			var xm1 = parseInt(pan_x * (1 - 1 / zoom) + coordo_x(evt.clientX) / zoom)
			var ym1 = parseInt(pan_y * (1 - 1 / zoom) + coordo_y(evt.clientY) / zoom)
			xm = xm1 * matrix.a + ym1 * matrix.c + matrix.e
			ym = xm1 * matrix.b + ym1 * matrix.d + matrix.f - 15
		}
	if (num == 1)
		{
		if (xm < 350) xm = 350
		if (xm > 620) xm = 620
		if (ym < 20) ym = 20
		if (ym > 170) ym = 170
		hue = (xm - 350) * 4 / 3
		saturate = (170 - ym) / 1.5
		svgdoc.getElementById("cursor1").setAttributeNS(null,"transform","translate("+(xm - 50) + "," + (ym - 120) +")")
		}
	if (num == 2)
		{
		if (ym < 20) ym = 20
		if (ym > 170) ym = 170
		luminosity = (170 - ym) / 1.5
		svgdoc.getElementById("cursor2").setAttributeNS(null,"transform","translate(200,"+ (ym - 120) +")")
		}
	rect_color = convertHSLtoRGB(evt,luminosity/100,saturate/100,hue/360)
	svgdoc.getElementById("test").setAttributeNS(null,"fill",rect_color)
	}
}

function linear_change_color(evt)
{
	var etiq = evt.target.getAttribute("id")
	var num_color = parseInt(etiq.substring(4, etiq.length))
	color[num_color] = rect_color
	svgdoc.getElementById("st" + num_color.toString()).setAttribute("stop-color",rect_color)
	svgdoc.getElementById("o" + num_color.toString()).setAttribute("fill",rect_color)
	svgdoc.getElementById("t_1_" + num_color.toString()).setAttribute("fill",rect_color)
	svgdoc.getElementById("t_2_" + num_color.toString()).setAttribute("fill",rect_color)
	svgdoc.getElementById("t_3_" + num_color.toString()).setAttribute("fill",rect_color)
}

function linear_supprime_color(evt)
{
	if (nb_stops <= 2)
	{
		add_alert("svg/alert.svg","outil2",dialog[30] + "\n" + dialog[31] + "\n" + dialog[32])
		return
	}
	var etiq = evt.target.getAttribute("id")
	var num_color = parseInt(etiq.substring(4, etiq.length))
	var node = svgdoc.getElementById("st" + num_color.toString())
	svgdoc.getElementById("MyGradient").removeChild(node)
	node = svgdoc.getElementById("o" + num_color.toString())
	svgdoc.getElementById("offsets").removeChild(node)
	node = svgdoc.getElementById("t_1_" + num_color.toString())
	svgdoc.getElementById("colors1").removeChild(node)
	node = svgdoc.getElementById("t_2_" + num_color.toString())
	svgdoc.getElementById("colors2").removeChild(node)
	node = svgdoc.getElementById("t_3_" + num_color.toString())
	svgdoc.getElementById("colors3").removeChild(node)
	if (num_color < nb_stops)
		for (var i = num_color; i < nb_stops; i ++)
		{
			color[i] = color[i + 1]
			stop[i] = stop[i + 1]
			svgdoc.getElementById("st" + (i + 1).toString()).setAttribute("id","st" + i.toString())
			svgdoc.getElementById("o" + (i + 1).toString()).setAttribute("id","o" + i.toString())
			svgdoc.getElementById("t_1_" + (i + 1).toString()).setAttribute("x", (330 + 20 * i).toString())
			svgdoc.getElementById("t_1_" + (i + 1).toString()).setAttribute("id","t_1_" + i.toString())
			svgdoc.getElementById("t_2_" + (i + 1).toString()).setAttribute("x", (330 + 20 * i).toString())
			svgdoc.getElementById("t_2_" + (i + 1).toString()).setAttribute("id","t_2_" + i.toString())
			svgdoc.getElementById("t_3_" + (i + 1).toString()).setAttribute("x", (330 + 20 * i).toString())
			svgdoc.getElementById("t_3_" + (i + 1).toString()).setAttribute("id","t_3_" + i.toString())
		}
	nb_stops -= 1
}


function linear_ajoute_color(evt)
{
	if (nb_stops >= 16)
	{
		add_alert("svg/alert.svg","outil2",dialog[33] + "\n" + dialog[34] + "\n" + dialog[35])
		return
	}
	var etiq = evt.target.getAttribute("id")
	var num_color = parseInt(etiq.substring(4, etiq.length))
	if (num_color < nb_stops)
		var offset = (stop[num_color] + stop[num_color + 1]) / 2
	else
		var offset = 1
	if (num_color < nb_stops)
		for (var i = nb_stops; i >= num_color; i --)
		{
			color[i + 1] = color[i]
			stop[i + 1] = stop[i]
			svgdoc.getElementById("st" + i.toString()).setAttribute("id","st" + (i + 1).toString())
			svgdoc.getElementById("o" + i.toString()).setAttribute("id","o" + (i + 1).toString())
			svgdoc.getElementById("t_1_" + i.toString()).setAttribute("x", (350 + 20 * i).toString())
			svgdoc.getElementById("t_1_" + i.toString()).setAttribute("id","t_1_" + (i + 1).toString())
			svgdoc.getElementById("t_2_" + i.toString()).setAttribute("x", (350 + 20 * i).toString())
			svgdoc.getElementById("t_2_" + i.toString()).setAttribute("id","t_2_" + (i + 1).toString())
			svgdoc.getElementById("t_3_" + i.toString()).setAttribute("x", (350 + 20 * i).toString())
			svgdoc.getElementById("t_3_" + i.toString()).setAttribute("id","t_3_" + (i + 1).toString())
		}
	else
		num_color += 1
	color[num_color] = rect_color
	stop[num_color] = offset
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","stop")
	node.setAttribute("id" , "st" + num_color.toString())
	node.setAttribute("offset" , (offset * 100).toString() + "%")
	node.setAttribute("stop-color" , rect_color)
	if (num_color < nb_stops)
		svgdoc.getElementById("MyGradient").insertBefore(node,svgdoc.getElementById("st" + (num_color + 1).toString()))
	else
		svgdoc.getElementById("MyGradient").appendChild(node)
	nb_stops += 1
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","circle")
	node.setAttribute("id" , "o" + num_color.toString())
	node.setAttribute("cx" , (460 + 200 * offset).toString())
	node.setAttribute("cy" , "370")
	node.setAttribute("r" , "5")
	node.setAttribute("fill" , rect_color)
	svgdoc.getElementById("offsets").appendChild(node)
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect")
	node.setAttribute("id" , "t_1_" + num_color.toString())
	node.setAttribute("x" , (330 + 20 * num_color).toString())
	node.setAttribute("y" , "260")
	node.setAttribute("width" , "20")
	node.setAttribute("height" , "20")
	node.setAttribute("fill" , rect_color)
	svgdoc.getElementById("colors1").appendChild(node)
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect")
	node.setAttribute("id" , "t_2_" + num_color.toString())
	node.setAttribute("x" , (330 + 20 * num_color).toString())
	node.setAttribute("y" , "310")
	node.setAttribute("width" , "20")
	node.setAttribute("height" , "20")
	node.setAttribute("fill" , rect_color)
	svgdoc.getElementById("colors2").appendChild(node)
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect")
	node.setAttribute("id" , "t_3_" + num_color.toString())
	node.setAttribute("x" , (330 + 20 * num_color).toString())
	node.setAttribute("y" , "360")
	node.setAttribute("width" , "20")
	node.setAttribute("height" , "20")
	node.setAttribute("fill" , rect_color)
	svgdoc.getElementById("colors3").appendChild(node)
}

function close_gradient()
{
	var node = svgdoc.getElementById("MyGradient")
	node.setAttribute("id", "grad" + (nbgradients + 1).toString())
	for (var i = 1; i <= nb_stops ; i ++)
		svgdoc.getElementById("st" + i.toString()).setAttribute("id" , "stop_" + nbgradients.toString() + "_" + i.toString())
	var node2 = node.cloneNode(true)
	svgdoc.getElementById("patterns").appendChild(node2)
	var node = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect")
	node.setAttribute("id" , "cases_grad" + (nbgradients + 1).toString())
	node.setAttribute("x" , (620 + 22 * (nbgradients % 8)).toString())
	node.setAttribute("y" ,  (400 + 22 * Math.floor(nbgradients / 8)).toString())
	node.setAttribute("width" , "20")
	node.setAttribute("height" , "20")
	node.setAttribute("fill" , "url(#grad" + (nbgradients + 1).toString() + ")")
	node.setAttribute("stroke" , "black")
	node.setAttribute("cursor" , "pointer")
	node.setAttribute("onclick" , "use_gradient(evt)")
	svgdoc.getElementById("gradient_cases").appendChild(node)
	nbgradients += 1
	remove_tool("gradient")
}